package net.xdclass.strategy;

import net.xdclass.common.WeightCategory;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * 短链码表位的生成
 */
public class ShardingTableConfig {

    /**
     * 存储数据表位置编号
     */
    private static final List<WeightCategory> tableSuffixList = new ArrayList<>();

    private static Random random = new Random();

    //配置启用那些表的后缀
    static {
        tableSuffixList.add(new WeightCategory("0", 30));
        tableSuffixList.add(new WeightCategory("a", 70));
    }


    /**
     * 获取随机的后缀
     * @return
     */
    public static String getRandomTableSuffix(String code) {
        // 第一种加权算法实现分配库表
        // return WeightConfig.getCategory(tableSuffixList);
        // 由于要对同一个URL生成固定的库表位，需要改进
        int hashCode = code.hashCode();
        int index = Math.abs(hashCode) % tableSuffixList.size();
        return tableSuffixList.get(index).getCategory();

    }


}
